package com.cictec.middleware;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.commons.cache.redis.RedisSingleConfiguration;
import com.cictec.middleware.commons.mybatis.dao.MybatisDaoUtil;
import com.cictec.middleware.commons.spring.SpringContextHolder;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@ImportResource(locations={"classpath:application-context.xml"})
@ComponentScan(excludeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, value = RedisSingleConfiguration.class),
		@Filter(type = FilterType.ASSIGNABLE_TYPE, value = MybatisDaoUtil.class) })
public class MiddlewareElasticsearchApplication {

	public static Logger logger = LoggerFactory.getLogger(MiddlewareElasticsearchApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(MiddlewareElasticsearchApplication.class, args);

		logger.debug("【middleware-elasticsearch】服务启动成功");

	}

	private static void test() {
		new Thread() {
			public void run() {
				while (true) {
					ss();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	private static void ss() {
		try {

			// ApplicationInfoManager sss =
			// ApplicationInfoManager.getInstance();
			// System.out.println(sss);

			EurekaDiscoveryClient clients = SpringContextHolder.getBean("discoveryClient");
			List<String> services = clients.getServices();
			for (String serviceId : services) {
				System.out.println(serviceId);
				List<ServiceInstance> serviceList = clients.getInstances(serviceId);
				for (ServiceInstance serviceInstance : serviceList) {
					System.out
							.println("" + "\t" + serviceInstance.getServiceId() + "\thost:" + serviceInstance.getHost()
									+ "\tport:" + serviceInstance.getPort() + "\turl:" + serviceInstance.getUri()
									+ "\tmetada:" + JSON.toJSONString(serviceInstance.getMetadata()));
				}
				System.out.println("--------------------------" + serviceList.size());

			}
			// System.out.println(clients);

			// EurekaClient client =
			// DiscoveryManager.getInstance().getEurekaClient();
			// //获取从Eureka获取的全部的应用列表
			// Applications apps = client.getApplications();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// System.out.println("o34k");
	}
}
